type ObjectFitOptions = {
  boxWidth: number,
  boxHeight: number,
  contentWidth: number,
  contentHeight: number,
  mode: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
}

export const getObjectFitSize = (options: ObjectFitOptions): { width: number, height: number } => {
  const { boxWidth, boxHeight, contentWidth, contentHeight, mode } = options;

  let width = contentWidth;
  let height = contentHeight;

  switch (mode) {
    case 'fill':
      // 直接填充容器，可能会拉伸
      width = boxWidth;
      height = boxHeight;
      break;

    case 'contain':
      // 保持比例缩放，适应容器
      const containRatio = Math.min(boxWidth / contentWidth, boxHeight / contentHeight);
      width = contentWidth * containRatio;
      height = contentHeight * containRatio;
      break;

    case 'cover':
      // 保持比例缩放，完全覆盖容器
      const coverRatio = Math.max(boxWidth / contentWidth, boxHeight / contentHeight);
      width = contentWidth * coverRatio;
      height = contentHeight * coverRatio;
      break;

    case 'none':
      // 不缩放，保持原始大小
      width = contentWidth;
      height = contentHeight;
      break;

    case 'scale-down':
      // 类似 contain，但如果内容原始大小小于容器则保持原大小
      const scaleDownRatio = Math.min(boxWidth / contentWidth, boxHeight / contentHeight);
      const scaledWidth = contentWidth * scaleDownRatio;
      const scaledHeight = contentHeight * scaleDownRatio;
      
      if (scaledWidth < contentWidth || scaledHeight < contentHeight) {
        width = scaledWidth;
        height = scaledHeight;
      } else {
        width = contentWidth;
        height = contentHeight;
      }
      break;
  }

  return { width, height };
}
